package controller.v2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import service.v2.OrderService;
import service.v2.PayService;
import util.PackageUtil;

@Controller
@RequestMapping("/v2/pay")
public class PaypalController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private PayService payService;
	
	/**
	 * 支付宝统一下单接口
	 * 
	 * @param request
	 * @param response
	 * @throws Exception 
	 * @attr param payType 1 余额充值 
	 */
	@ResponseBody
	@RequestMapping("/paypalPrePay")																							
	public Object paypalPrePay(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id ,  
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(request.getAttribute("payType")==null){
			return PackageUtil.packageParams(403,"forbidden","",""); 
		}
		int type = (Integer)request.getAttribute("payType");
		System.out.println("请求URL********************"+request.getRequestURL().toString());
		System.out.println("请求URI********************"+request.getRequestURI().toString());
		String no = 	(String) request.getAttribute("order_no");
		String price = 	(String) request.getAttribute("price");
		String body = 	(String) request.getAttribute("body");
		String cancel_url = 	(String) request.getAttribute("cancel_url");
		String return_url = 	(String) request.getAttribute("return_url");
		String url = "";
		if (type == 1) {// 余额充值
//			url = "pay/rechargePayssionPayNotify";
		} else if (type == 2) {// 兑换券充值
			url = "pay/ticketPaypalNotify";
		} else if (type == 3) {// 订单支付
			url = "pay/OrderPayPalNotify";
		} else if (type == 4) {// 套餐购买
			url = "pay/packagePayPalNotify";
		}
		return payService.paypalPrePay(no, price ,body, request,url, return_url, cancel_url);
	}
	
	
}
